package com.yrot.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrot.admin.entity.SysQuestionBank;
import com.yrot.admin.entity.SysQuestions;
import com.yrot.admin.form.ExpertBankResult;
import com.yrot.admin.service.ISysQuestionBankService;
import com.yrot.admin.service.ISysQuestionsService;
import com.yrot.common.annotation.SysLog;
import com.yrot.common.result.R;
import com.yrot.common.result.Result;
import com.yrot.common.result.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hemin
 * @since 2019-04-02
 */
@RestController
@RequestMapping("/admin/questionBank")
public class SysQuestionBankController extends BaseController{

    @Autowired
    ISysQuestionBankService questionBankService;

    @Autowired
    ISysQuestionsService questionsService;

    @GetMapping("/banks")
    public Result questionBanks(int page,int limit,
                                @RequestParam(required = false) String bankName,
                                @RequestParam(required = false) String isPublic){
        QueryWrapper<SysQuestionBank> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(bankName)){
            queryWrapper.like("bank_name",bankName);
        }
        if(StringUtils.isNotEmpty(isPublic)){
            queryWrapper.eq("is_public",isPublic);
        }
        queryWrapper.eq("expert_id",sysExpert().getExpertId());
        queryWrapper.orderByDesc("bank_id");
        Page<SysQuestionBank> pages = new Page<>(page, limit);
        IPage<SysQuestionBank> questionBanks1 = questionBankService.page(pages, queryWrapper);
        return ResultUtils.success(questionBanks1.getTotal(),questionBanks1.getRecords());
    }

    /**
     * 查询该专家的所有题库
     * @return 题库列表
     */
    @GetMapping("/expertBanks")
    public List<SysQuestionBank> questionBanks(){
        QueryWrapper<SysQuestionBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("expert_id",sysExpert().getExpertId());
        List<SysQuestionBank> list = questionBankService.list(queryWrapper);
        return list;
    }

    /**
     * 查询该专家的所有题库
     * @return 题库列表
     */
    @GetMapping("/expertBanksNum")
    public List<ExpertBankResult> questionBanksNum(){
        QueryWrapper<SysQuestionBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("expert_id",sysExpert().getExpertId());
        queryWrapper.or();
        queryWrapper.eq("is_public",1);
        List<SysQuestionBank> list = questionBankService.list(queryWrapper);
        ArrayList<ExpertBankResult> expertBankResults = new ArrayList<>();
        list.forEach(bank->{
            ExpertBankResult expertBankResult = new ExpertBankResult();
            BeanUtils.copyProperties(bank,expertBankResult);
            QueryWrapper<SysQuestions> questionsQueryWrapper = new QueryWrapper<>();
            questionsQueryWrapper.eq("bank_id",expertBankResult.getBankId());
            expertBankResult.setQuestionNum(questionsService.count(questionsQueryWrapper));
            if(bank.getIsPublic() == 1){
                expertBankResult.setBankName(expertBankResult.getBankName()+"(公共题库)");
            }
            expertBankResults.add(expertBankResult);
        });
        return expertBankResults;
    }

    @PostMapping("/bank")
    @SysLog("新增题库")
    public R saveBank(@RequestParam(value = "bankName")String bankName,
                      @RequestParam(value = "bankIntroduction") String bankIntroduction,
                      @RequestParam(value = "isPublic",required = false,defaultValue = "0") Integer isPublic){
        Integer expertId = sysExpert().getExpertId();
        QueryWrapper<SysQuestionBank> bankQueryWrapper = new QueryWrapper<>();
        bankQueryWrapper.eq("expert_id", expertId);
        bankQueryWrapper.eq("bank_name", bankName);
        SysQuestionBank bank = questionBankService.getOne(bankQueryWrapper);
        if(bank != null){
            return R.error("该题库已经存在，请勿重复");
        }
        SysQuestionBank sysQuestionBank = new SysQuestionBank();
        sysQuestionBank.setExpertId(expertId);// 专家
        sysQuestionBank.setBankIntroduction(bankIntroduction).setBankName(bankName).setIsPublic(isPublic);
        questionBankService.save(sysQuestionBank);
        return R.ok();
    }

    @DeleteMapping("/bank/{bankId}")
    @SysLog("删除题库")
    public void removeBank(@PathVariable Integer bankId){
        questionBankService.removeBank(bankId);

    }

    @PutMapping("/bank")
    @SysLog("修改题库")
    public Result questionBanks(SysQuestionBank questionBank){
        if(questionBank.getIsPublic() == null){
            questionBank.setIsPublic(0);
        }
        questionBankService.saveOrUpdate(questionBank);
        return ResultUtils.success();
    }

}
